//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package net.mingsoft.mdiy.biz.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.map.CaseInsensitiveMap;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import net.mingsoft.base.biz.impl.BaseBizImpl;
import net.mingsoft.base.dao.IBaseDao;
import net.mingsoft.base.util.SqlInjectionUtil;
import net.mingsoft.basic.util.BasicUtil;
import net.mingsoft.mdiy.biz.IModelBiz;
import net.mingsoft.mdiy.biz.IModelDataBiz;
import net.mingsoft.mdiy.dao.IModelDao;
import net.mingsoft.mdiy.entity.ModelEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ModelDataImpl extends BaseBizImpl implements IModelDataBiz {
    @Autowired
    private IModelBiz modelBiz;
    @Autowired
    private IModelDao modelDao;

    public ModelDataImpl() {
    }

    protected IBaseDao getDao() {
        return this.modelDao;
    }

    public boolean saveDiyFormData(String modelId, Map<String, Object> params) {
        ModelEntity model = (ModelEntity)this.modelBiz.getById(modelId);
        JSONArray fieldArray = JSONUtil.parseArray(model.getModelField());
        Iterator var5 = fieldArray.iterator();

        while(var5.hasNext()) {
            Object field = var5.next();
            JSONObject obj = (JSONObject)field;
            if ("autoid".equalsIgnoreCase(obj.getStr("type")) && obj.get("fmt") != null) {
                JSONObject fmt = obj.getJSONObject("fmt");
                String separator = fmt.getStr("separator", "");
                String fieldValue = fmt.getStr("prefix") + (StringUtils.isNotEmpty(fmt.getStr("dateFormat")) ? separator + DateUtil.format(new Date(), fmt.getStr("dateFormat")) : "");
                if (fmt.getBool("isSpliceId", false)) {
                    int count = this.modelBiz.countBySQL(model.getModelTableName(), new HashMap());
                    fieldValue = fieldValue + separator + (count + 1);
                }

                params.put(obj.getStr("model"), fieldValue);
            }
        }

        if (ObjectUtil.isNotNull(model)) {
            Map fieldMap = model.getFieldMap();
            HashMap<String, Object> fields = new HashMap();
            Iterator var16 = params.keySet().iterator();

            while(var16.hasNext()) {
                String s = (String)var16.next();
                if (fieldMap.containsKey(s)) {
                    try {
                        DateTime dateTime = DateUtil.parseDateTime(params.get(s).toString());
                        fields.put(fieldMap.get(s).toString(), dateTime);
                    } catch (Exception var12) {
                        fields.put(fieldMap.get(s).toString(), params.get(s));
                    }
                }
            }

            fields.put("CREATE_DATE", new Date());
            if (model.getModelIdType() == 0) {
                Snowflake snowflake = IdUtil.getSnowflake();
                fields.put("ID", snowflake.nextId());
            }

            if (BasicUtil.getManager() != null) {
                fields.put("CREATE_BY", BasicUtil.getManager().getId());
            }

            this.modelBiz.insertBySQL(model.getModelTableName(), fields);
            return true;
        } else {
            return false;
        }
    }

    public boolean updateDiyFormData(String modelId, Map<String, Object> params) {
        ModelEntity model = (ModelEntity)this.modelBiz.getById(modelId);
        if (ObjectUtil.isNotNull(model)) {
            Map fieldMap = model.getFieldMap();
            HashMap<String, Object> fields = new HashMap();
            Iterator var6 = params.keySet().iterator();

            while(var6.hasNext()) {
                String s = (String)var6.next();
                if (fieldMap.containsKey(s)) {
                    fields.put(fieldMap.get(s).toString(), params.get(s));
                }
            }

            if (StringUtils.isEmpty(params.get("id").toString())) {
                this.LOG.debug("请求数据不含主键id,无法更新");
                return false;
            } else {
                fields.put("UPDATE_DATE", new Date());
                fields.put("UPDATE_BY", new Date());
                Map<String, String> where = new HashMap();
                where.put("id", params.get("id").toString());
                this.modelBiz.updateBySQL(model.getModelTableName(), fields, where);
                return true;
            }
        } else {
            return false;
        }
    }

    public List queryDiyFormData(String modelId, Map<String, Object> params) {
        ModelEntity model = (ModelEntity)this.modelBiz.getById(modelId);
        if (ObjectUtil.isNotNull(model)) {
            Map fieldMap = model.getFieldMap();
            HashMap<String, Object> fields = new HashMap();
            Iterator var6 = params.keySet().iterator();

            String orderby;
            while(var6.hasNext()) {
                orderby = (String)var6.next();
                if (fieldMap.containsKey(orderby)) {
                    fields.put(fieldMap.get(orderby).toString(), params.get(orderby));
                }
            }

            List<Map> sqlWhereList = null;
            if (params.get("sqlWhere") != null) {
                sqlWhereList = JSONUtil.toList(params.get("sqlWhere").toString(), Map.class);
            }

            orderby = null;
            if (params.get("orderBy") != null) {
                orderby = params.get("orderBy").toString();
            }

            String order = null;
            if (params.get("order") != null) {
                order = params.get("order").toString();
            }

            BasicUtil.startPage();
            SqlInjectionUtil.filterContent(new String[]{orderby});
            List list = this.modelBiz.queryBySQL(model.getModelTableName(), (List)null, fields, sqlWhereList, orderby, order);
            return list;
        } else {
            return null;
        }
    }

    public Object getFormData(String modelId, String id) {
        if (!StringUtils.isEmpty(id) && !StringUtils.isEmpty(modelId)) {
            ModelEntity model = (ModelEntity)this.modelBiz.getById(modelId);
            if (model == null) {
                this.LOG.debug("模型不存在");
                return null;
            } else {
                HashMap<String, Object> fields = new HashMap();
                fields.put("id", id);
                List<Map<String, Object>> list = this.modelBiz.queryBySQL(model.getModelTableName(), (List)null, fields);
                if (CollUtil.isEmpty(list)) {
                    return null;
                } else {
                    HashMap<String, Object> modelEntity = new HashMap();
                    Map<String, Object> fieldMap = model.getFieldMap();
                    Iterator var8 = ((Map)list.get(0)).keySet().iterator();

                    while(var8.hasNext()) {
                        String s = (String)var8.next();
                        Iterator var10 = fieldMap.entrySet().iterator();

                        while(var10.hasNext()) {
                            Entry<String, Object> entry = (Entry)var10.next();
                            if (s.equalsIgnoreCase(entry.getValue().toString())) {
                                modelEntity.put(entry.getKey(), ((Map)list.get(0)).get(s));
                            }
                        }
                    }

                    modelEntity.put("id", id);
                    return modelEntity;
                }
            }
        } else {
            this.LOG.debug("模型或主键参数为空");
            return null;
        }
    }

    public void deleteQueryDiyFormData(String id, String diyFormId) {
        ModelEntity model = (ModelEntity)this.modelBiz.getById(diyFormId);
        if (ObjectUtil.isNotNull(model)) {
            HashMap hashMap = new HashMap();
            hashMap.put("id", id);
            this.modelBiz.deleteBySQL(model.getModelTableName(), hashMap);
        }

    }

    public int countDiyFormData(String modelId, Map<String, Object> params) {
        ModelEntity model = (ModelEntity)this.modelBiz.getById(modelId);
        if (ObjectUtil.isNotNull(model)) {
            HashMap fields = new HashMap();
            Map fieldMap = model.getFieldMap();
            Iterator var6 = params.keySet().iterator();

            while(var6.hasNext()) {
                String s = (String)var6.next();
                if (fieldMap.containsKey(s)) {
                    fields.put(fieldMap.get(s).toString(), "'" + params.get(s) + "'");
                }
            }

            return this.modelBiz.countBySQL(model.getModelTableName(), fields);
        } else {
            return 0;
        }
    }
}
